package com.hnist.empdemo.view.component;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;


/**
 * 男女比例饼图组件
 */
public class GenderRatioPieChart{

    // 饼图数据
    private ObservableList<PieChart.Data> pieChartData;

    // 组件标题
    private String title;

    // 默认数据
    private double malePercentage = 50.0;
    private double femalePercentage = 50.0;

    /**
     * 构造函数
     */
    public GenderRatioPieChart() {
        this("男女比例分布图");
    }

    /**
     * 带标题的构造函数
     * @param title 图表标题
     */
    public GenderRatioPieChart(String title) {
        this.title = title;
        initializeData();
    }

    /**
     * 初始化饼图数据
     */
    private void initializeData() {
        pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("男性 " + String.format("%.1f", malePercentage) + "%", malePercentage),
                new PieChart.Data("女性 " + String.format("%.1f", femalePercentage) + "%", femalePercentage)
        );
    }

    /**
     * 设置男女比例数据
     * @param male 男性比例(0-100)
     * @param female 女性比例(0-100)
     */
    public void setData(double male, double female) {
        // 确保比例之和为100%
        double total = male + female;
        if (total > 0) {
            this.malePercentage = (male / total) * 100;
            this.femalePercentage = (female / total) * 100;
        } else {
            this.malePercentage = 0;
            this.femalePercentage = 0;
        }

        // 更新数据
        pieChartData.clear();
        pieChartData.addAll(
                new PieChart.Data("男性 " + String.format("%.1f", malePercentage) + "%", malePercentage),
                new PieChart.Data("女性 " + String.format("%.1f", femalePercentage) + "%", femalePercentage)
        );
    }

    /**
     * 创建饼图组件
     * @return 包含饼图的VBox容器
     */
    public VBox createChart() {
        // 创建饼图
        PieChart pieChart = new PieChart(pieChartData);
        pieChart.setTitle(title);
        pieChart.setLabelLineLength(10);
        pieChart.setLabelsVisible(true);
        pieChart.setPadding(new Insets(20));

        // 可以设置饼图的颜色
        int index = 0;
        for (PieChart.Data data : pieChart.getData()) {
            if (index == 0) {
                data.getNode().setStyle("-fx-pie-color: #3498db;"); // 男性 - 蓝色
            } else {
                data.getNode().setStyle("-fx-pie-color: #e74c3c;"); // 女性 - 红色
            }
            index++;
        }

        // 创建标题标签
        Label headerLabel = new Label(title);
        headerLabel.setFont(new Font("Arial", 18));
        headerLabel.setPadding(new Insets(10, 0, 10, 0));

        // 创建容器并添加组件
        VBox vbox = new VBox(10);
        vbox.setPadding(new Insets(10));
        vbox.getChildren().addAll(headerLabel, pieChart);

        return vbox;
    }
}
